package me.macd.mindoc.dao.impl;

import me.macd.mindoc.dao.RelationshipDao;
import me.macd.mindoc.model.Relationship;
import me.macd.mindoc.util.DefaultNameHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.xml.soap.SAAJResult;
import java.util.List;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-10-31
 * @create: 2018-10-31 19:55
 */
@Repository
public class RelationshipDaoJDBCTemplateImpl implements RelationshipDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public RelationshipDaoJDBCTemplateImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int insert(Relationship relationship) {
        return new CommonDao(jdbcTemplate).insert(relationship);
    }

    @Override
    public List<Relationship> findListByBookId(int bookId, int memberId) {
        String sql = "SELECT * FROM md_relationship WHERE book_id=? AND member_id=?";
        return jdbcTemplate.query(sql, new Object[]{bookId, memberId}, new CommonRowMapper<>(Relationship.class, new DefaultNameHandler()));
    }

    @Override
    public List<Relationship> findListByBookIdAndRoleId(int bookId, int roleId) {
        String sql = "SELECT * FROM md_relationship WHERE book_id=? AND role_id=?";
        return jdbcTemplate.query(sql, new Object[]{bookId, roleId}, new CommonRowMapper<>(Relationship.class, new DefaultNameHandler()));
    }
}
